package com.example.create_excel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataService {
    private final DataMapper dataMapper;

    @Autowired
    public DataService(DataMapper dataMapper) {
        this.dataMapper = dataMapper;
    }

    public List<Map<String, String>> getData_ywlj() {
        return disposeData(dataMapper.getData_ywlj());
    }

    public List<Map<String, String>> getData_fgj() {
        return disposeData(dataMapper.getData_fgj());
    }

    private List<Map<String, String>> disposeData(List<Map<String, String>> data) {
        Map<String, Integer> sum = new HashMap<>();
        Map<String, Float> notUpdate_sum = new HashMap<>();
        for (Map<String, String> datum : data) {
            Set<String> keys = sum.keySet();
            String key = datum.get("COMPANYNAME");
            String notUpdate_key = datum.get("NOTUPDATEDAY");
            if (keys.contains(key)) {
                sum.put(key, sum.get(key) + 1);
                if (notUpdate_key != null)
                    notUpdate_sum.put(key, notUpdate_sum.get(key) + disposeTime(notUpdate_key));
                else
                    datum.put("NOTUPDATEDAY", "-");
            } else {
                sum.put(key, 1);
                if (notUpdate_key != null)
                    notUpdate_sum.put(key, disposeTime(notUpdate_key));
                else {
                    notUpdate_sum.put(key, 0f);
                    datum.put("NOTUPDATEDAY", "-");
                }
            }
        }
        for (int i = 0; i < data.size(); ++i) {
            int s = sum.get(data.get(i).get("COMPANYNAME"));
            float ss = notUpdate_sum.get(data.get(i).get("COMPANYNAME")) / s;
            //String time = data.get(i).get("UPDATETIME").equals("-") ? "-" : data.get(i).get("UPDATETIME").substring(0,data.get(i).get("UPDATETIME").indexOf("."));
            String time = data.get(i).get("UPDATETIME");
            String notUpdate_time = data.get(i).get("NOTUPDATEDAY").equals("-") ? "-" : String.valueOf(disposeTime(data.get(i).get("NOTUPDATEDAY")));
            data.get(i).put("NO", String.valueOf(i + 1));
            data.get(i).put("DIRNUMBER", String.valueOf(s));
            data.get(i).put("UPDATETIME", time);
            data.get(i).put("NOTUPDATEDAY", notUpdate_time);
            data.get(i).put("AVENOTUPDATEDAY", String.valueOf(ss));
        }
        return data;
    }

    private float disposeTime(String time) {
        float res;
        int day = Integer.parseInt(time.substring(0, time.indexOf("天")));
        int hour = Integer.parseInt(time.substring(time.indexOf("天") + 1, time.indexOf("时")));
        int minute = Integer.parseInt(time.substring(time.indexOf("时") + 1, time.indexOf("分")));
        res = day + hour / 24f + minute / 1440f;
        return res;
    }



    private List<Map<String, String>> getContext() {
        List<Map<String, String>> context = new ArrayList<>();
        Map<String, String> element1 = new HashMap<>();
        element1.put("NO", "1");
        element1.put("COMPANYNAME", "委办公室");
        element1.put("DIRNUMBER", "1");
        element1.put("DIRNAME", "委综合办文信息");
        element1.put("UPDATETIME", "2021/2/1 15:42:59");
        element1.put("NOTUPDATEDAY", "4");
        element1.put("MONTHLYDATANUMBER", "");
        element1.put("SUMDATANUMBER", "");
        element1.put("AVENOTUPDATEDAY", "4");
        Map<String, String> element2 = new HashMap<>();
        element2.put("NO", "2");
        element2.put("COMPANYNAME", "委城管处");
        element2.put("DIRNUMBER", "3");
        element2.put("DIRNAME", "无证建筑信息");
        element2.put("UPDATETIME", "2021/2/2 22:29:08");
        element2.put("NOTUPDATEDAY", "");
        element2.put("MONTHLYDATANUMBER", "");
        element2.put("SUMDATANUMBER", "");
        element2.put("AVENOTUPDATEDAY", "13.0630555555666");
        Map<String, String> element3 = new HashMap<>();
        element3.put("NO", "3");
        element3.put("COMPANYNAME", "委城管处");
        element3.put("DIRNUMBER", "3");
        element3.put("DIRNAME", "无证建筑任务执行信息");
        element3.put("UPDATETIME", "2021/2/2 22:29:14");
        element3.put("NOTUPDATEDAY", "2.0630324074009");
        element3.put("MONTHLYDATANUMBER", "");
        element3.put("SUMDATANUMBER", "");
        element3.put("AVENOTUPDATEDAY", "13.0630555555666");
        Map<String, String> element4 = new HashMap<>();
        element4.put("NO", "4");
        element4.put("COMPANYNAME", "委城管处");
        element4.put("DIRNUMBER", "3");
        element4.put("DIRNAME", "无证建筑统计报表信息");
        element4.put("UPDATETIME", "2020/12/31 22:29:14");
        element4.put("NOTUPDATEDAY", "35.0630324074009");
        element4.put("MONTHLYDATANUMBER", "");
        element4.put("SUMDATANUMBER", "");
        element4.put("AVENOTUPDATEDAY", "13.0630555555666");
        Map<String, String> element5 = new HashMap<>();
        element5.put("NO", "5");
        element5.put("COMPANYNAME", "委行政服务中心");
        element5.put("DIRNUMBER", "2");
        element5.put("DIRNAME", "上海市建设市场管理信息平台安全生产许可证");
        element5.put("UPDATETIME", "2021/2/3  2:00:36");
        element5.put("NOTUPDATEDAY", "1.91625000000204");
        element5.put("MONTHLYDATANUMBER", "");
        element5.put("SUMDATANUMBER", "");
        element5.put("AVENOTUPDATEDAY", "20.6642389945708");
        Map<String, String> element6 = new HashMap<>();
        element6.put("NO", "6");
        element6.put("COMPANYNAME", "委行政服务中心");
        element6.put("DIRNUMBER", "2");
        element6.put("DIRNAME", "上海市建设市场管理信息平台工地安标竣工确认");
        element6.put("UPDATETIME", "2021/2/3  2:04:08");
        element6.put("NOTUPDATEDAY", "1.91379629629955");
        element6.put("MONTHLYDATANUMBER", "");
        element6.put("SUMDATANUMBER", "");
        element6.put("AVENOTUPDATEDAY", "20.6642389945708");
        context.add(element1);
        context.add(element2);
        context.add(element3);
        context.add(element4);
        context.add(element5);
        context.add(element6);
        return context;
    }
}
